package client;
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ConnectException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author MohamedNour
 */
public class ClientSocket implements Runnable {

    private String address;
    private int port;
    private Socket socket;
    private DataOutputStream out;
    private DataInputStream in;
    private Client client;
    
    public ClientSocket(String address, int port, Client client) {
        this.address = address;
        this.port = port;
        this.client = client;
    }

    public void connect() {
            try
            {
                socket = new Socket(address, port);
                out = new DataOutputStream(socket.getOutputStream());
                in = new DataInputStream(socket.getInputStream());
            } catch (ConnectException ex) {
                System.err.println("Connection time out exception to " + address);
            } catch (UnknownHostException e) {
                e.printStackTrace();
                System.err.println("Don't know about host: " + address);
                System.exit(1);
            } catch (IOException e) {
                e.printStackTrace();
                System.err.println("Couldn't get I/O for the connection to: " + address);
                System.exit(1);
            }
    }

    public void run()
    {
    	startReceiving();
    }
    
    public void startReceiving()
    {
    	while(true)
        {
            try {
                int messageLength = Integer.parseInt(in.readUTF());
                System.out.println("Before receiving message with " + messageLength);
                byte[] message = new byte[messageLength];
                in.readFully(message);
                System.out.println("After receiving message with " + messageLength + " \n " + Arrays.toString(message));
                client.handleMessage(message);
            } catch (IOException ex) {
                Logger.getLogger(ClientSocket.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    
    public String receiveData()
    {
    	try{
    		return in.readUTF();
	        //TODO handle client's received message here
	    } catch (IOException ex) {
	        Logger.getLogger(ClientSocket.class.getName()).log(Level.SEVERE, null, ex);
	        ex.printStackTrace();
	    }
        return null;
    }

    public void receiveBytes(byte data[]) {
    	try{
                in.read(data);
	        //TODO handle client's received message here
	    } catch (IOException ex) {
	        Logger.getLogger(ClientSocket.class.getName()).log(Level.SEVERE, null, ex);
	        ex.printStackTrace();
	    }
    }

    public void sendMessage(String message)
    {
        try {
            System.out.println("Sending message with " + message);
            out.writeUTF(message);
            out.flush();
        } catch (IOException ex) {
            Logger.getLogger(ClientSocket.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void sendMessage(byte[] message)
    {
        try {
            System.out.println("Sending message with " + message);
            out.write(message);
            out.flush();
        } catch (IOException ex) {
            Logger.getLogger(ClientSocket.class.getName()).log(Level.SEVERE, null, ex);
            ex.printStackTrace();
        }
    }
    
}
